#include "AppleDropGame.h"

void AppleDropGame::GameInit()
{
	gTheMouse.Initialise(SCREENHEIGHT/2, SCREENWIDTH/2);

	startButton.Initialise(SCREENHEIGHT-100,SCREENWIDTH-370,SCREENHEIGHT-50,SCREENWIDTH-220, "START");

	endButton.Initialise(SCREENHEIGHT-100,SCREENWIDTH-200,SCREENHEIGHT-50,SCREENWIDTH-50, "END");

	score = 0;

	highScore = 0;//change this later

	theZones[0].theZone.PlaceAt(Vector2D(119,345),80);
	theZones[1].theZone.PlaceAt(Vector2D(350,280),75);
	theZones[2].theZone.PlaceAt(Vector2D(535,375),58);
	theZones[3].theZone.PlaceAt(Vector2D(765,215),90);

	for (int i = 0; i < maxApples; i++)
	{
		apples[i].isActive = false;
		apples[i].apple_Size.XValue = 20;
		apples[i].apple_Size.YValue = 20;

		apples[i].apple.init(Vector2D(0,0),apples[i].apple_Size,"AppleDrop/apple.png");
	}

	catcher_Position.XValue = SCREENHEIGHT - 50;
	catcher_Position.YValue = SCREENWIDTH/2+180;

	catcher_Velocity.XValue = 0;
	catcher_Velocity.YValue = 0;

	catcher_Size.XValue = 60;
	catcher_Size.YValue = 60;

	theState = STARTUP;

	timeTillNextSpawn = 0;
	spawnNew = false;

	setGameActive(true);

	catcher.init(catcher_Position,catcher_Size,"AppleDrop/basket.png");

	backdrop.init(Vector2D(SCREENWIDTH/2,SCREENHEIGHT/2),Vector2D(SCREENWIDTH,SCREENHEIGHT),"AppleDrop/backdrop.png");
}

void AppleDropGame::GameMain()
{
	backdrop.Draw();

	if(theState == STARTUP)
	{
		startButton.Update(gTheMouse);
		startButton.Draw();

		endButton.Update(gTheMouse);
		endButton.Draw();

		TextWriter(Vector2D(30,50),"HIGHSCORE: ",MyDrawEngine::DARKGREEN);
		IntWriter(Vector2D(170,50), highScore ,MyDrawEngine::DARKGREEN);

		gTheMouse.Update(SCREENWIDTH, SCREENHEIGHT);
		gTheMouse.Draw();

		if (startButton.getActive())
		{
			timer = 120;

			theState = INGAME;
		}

		if (endButton.getActive())
		{
			theState = WRAPUP;
		}
	}
	else if (theState == INGAME)
	{
		theGameLoop();

		if (timer == 0)
		{
			if(score > highScore)
			{
				highScore = score;
			}

			for (int i = 0; i < maxApples; i++)
			{
				apples[i].isActive = false;
			}

			score = 0;

			catcher_Position.XValue = SCREENHEIGHT - 50;
			catcher_Position.YValue = SCREENWIDTH/2+180;

			catcher_Velocity.XValue = 0;
			catcher_Velocity.YValue = 0;

			theState = STARTUP;
		}
	}
	else if (theState == WRAPUP)
	{
		GameShutdown();
	}
	else
	{
		GameShutdown();
	}


}

void AppleDropGame::GameShutdown()
{	
	setGameActive(false);

	theResult.score = highScore;
}

void AppleDropGame::theGameLoop()
{
	if (KEYPRESSED('A') && catcher_Velocity.XValue > -10)
	{
		catcher_Velocity.XValue = catcher_Velocity.XValue - 1;
	}
	else if (KEYPRESSED('D') && catcher_Velocity.XValue < 10)
	{
		catcher_Velocity.XValue = catcher_Velocity.XValue + 1;
	}
	else
	{
		if (catcher_Velocity.XValue > 0)
		{
			catcher_Velocity.XValue = catcher_Velocity.XValue - 1;
		}	
		else if (catcher_Velocity.XValue < 0)
		{
			catcher_Velocity.XValue = catcher_Velocity.XValue + 1;
		}
	}

	if (catcher_Position.XValue + catcher_Velocity.XValue > SCREENWIDTH ||
		catcher_Position.XValue + catcher_Velocity.XValue < 0)
	{
		catcher_Velocity.XValue = 0;
	}

	catcher_Position += catcher_Velocity;

	catcher.Update(catcher_Velocity);
	catcher.Draw();

	TextWriter(Vector2D(30,50),"SCORE: ",MyDrawEngine::DARKGREEN);
	IntWriter(Vector2D(130,50), score ,MyDrawEngine::DARKGREEN);

	TextWriter(Vector2D(30,70),"TIME: ",MyDrawEngine::DARKGREEN);
	IntWriter(Vector2D(130,70), timer ,MyDrawEngine::DARKGREEN);

	if(timeTillNextSpawn <= 0)
	{
		spawnNew = true;
	}

	if (spawnNew)
	{
		int newApple = -1;

		for (int i = 0; i < maxApples; i++)
		{
			if (apples[i].isActive == false)
			{
				apples[i].isActive = true;
				newApple = i;
				break;
			}
		}

		if (newApple == -1)
		{
			spawnNew = false;
		}

		if (spawnNew)
		{

			int lastActive = 200;
			int lastZone = -1;

			for (int i = 0; i < 4; i++)
			{
				if (theZones[i].zoneTimer < lastActive)
				{
					lastActive = theZones[i].zoneTimer;
					lastZone = i;
				}
			
				if (theZones[i].zoneTimer == lastActive && rand()%3 == 0)
				{
					lastActive = theZones[i].zoneTimer;
					lastZone = i;
				}
			}

			for (int i = 0; i < 4; i++)
			{
				if (i == lastZone)
				{
					theZones[i].zoneTimer += 1;
				}
				else
				{
					theZones[i].zoneTimer -= 1;
				}
			}

			apples[newApple].apple_Position.XValue = (theZones[lastZone].theZone.GetCentre().XValue
				+ (rand()%((int)theZones[lastZone].theZone.GetRadius()*2)
				- (int)theZones[lastZone].theZone.GetRadius()));

			apples[newApple].apple_Position.YValue = (theZones[lastZone].theZone.GetCentre().YValue
				+ (rand()%((int)theZones[lastZone].theZone.GetRadius()*2)
				- (int)theZones[lastZone].theZone.GetRadius()));

			apples[newApple].apple_Velocity.XValue = 0;

			apples[newApple].apple_Velocity.YValue = float(rand()%5+1);

			spawnNew = false;

			timeTillNextSpawn = rand()%50+20;
		}
	}

	for (int i = 0; i < maxApples; i++)
	{
		if (apples[i].isActive)
		{
			apples[i].apple_Position = apples[i].apple_Position + apples[i].apple_Velocity;

			if (apples[i].apple_Position.YValue > catcher_Position.YValue + 40)
			{
				score -= 10;
				apples[i].isActive = false;
			}
			if (apples[i].apple_Position.YValue >= catcher_Position.YValue &&
				apples[i].apple_Position.YValue <= catcher_Position.YValue + (catcher_Size.YValue/2) &&
				apples[i].apple_Position.XValue >= catcher_Position.XValue - (catcher_Size.XValue/2) &&
				apples[i].apple_Position.XValue <= catcher_Position.XValue + (catcher_Size.XValue/2))
			{
				apples[i].isActive = false;
				score += 20;
			}

			if (apples[i].isActive)
			{
				apples[i].apple.setPosition(apples[i].apple_Position);

				apples[i].apple.Draw();
			}

		}
	}

	timeTillNextSpawn -=1;

	if (theClock.ticker())
	{
		timer -= 1;
	}
}

GameResult AppleDropGame::GetResult()
{
	return theResult;
}

Game* GetAppledropGame()
{
	return new AppleDropGame;
}